package com.bizfx.component.controls
{
	import flash.events.DataEvent;
	import flash.events.Event;
	import flash.events.IOErrorEvent;
	import flash.events.ProgressEvent;
	import flash.events.SecurityErrorEvent;
	import flash.net.FileFilter;
	import flash.net.FileReference;
	import flash.net.URLRequest;
	
	import mx.containers.Canvas;
	import mx.controls.Alert;
	import mx.controls.ProgressBar;

	public class FileUploader extends Canvas
	{
		private var _fileFilterString:String = "*.*";
		/**
		 * 文件过滤
		 */
		public function get fileFilterString():String
		{
			return _fileFilterString;
		}

		public function set fileFilterString(value:String):void
		{
			_fileFilterString = value;
		}

		private var _maxFileSize:int = 10*1024*1024;
		/**
		 * 最大上传文件
		 */
		public function get maxFileSize():int
		{
			return _maxFileSize;
		}

		public function set maxFileSize(value:int):void
		{
			if (value < 0) {
				_maxFileSize = 0;
			} else {
				_maxFileSize = value;
			}
		}

		private var _serverURL:String = "";
		/**
		 * 服务器地址
		 */
		public function get serverURL():String
		{
			return _serverURL;
		}

		public function set serverURL(value:String):void
		{
			_serverURL = value;
		}
		
		private var file:FileReference;
		
		private var progressBar:ProgressBar;
		
		public function FileUploader()
		{
			file = new FileReference();
			progressBar = new ProgressBar();
		}
		
		public function browse():void{ 
			var filter:FileFilter = new FileFilter(fileFilterString, fileFilterString);
			file.browse([filter]);
			file.addEventListener(Event.SELECT, selectHandler);
		}
		
		public function upLoad():void{ 
			var url:URLRequest = new URLRequest(this.serverURL); 
			file.upload(url);
		}
		
		private function selectHandler(event:Event):void{ 
			var f:FileReference = FileReference(event.target); 
			if(f.size > maxFileSize){
				Alert.show("文件太大,请确保文件在10M以内的"); 
			} else {
				file.addEventListener(ProgressEvent.PROGRESS, progressHandler);
				file.addEventListener(Event.COMPLETE, uploadCompleteHandler); 
				file.addEventListener(IOErrorEvent.IO_ERROR, uploadIOErrorHandler);
				file.addEventListener(SecurityErrorEvent.SECURITY_ERROR, uploadSecurityErrorHandler);
				file.addEventListener(DataEvent.UPLOAD_COMPLETE_DATA, uploadCompleteDataHandler);
			}
		}
		
		private function progressHandler(event:ProgressEvent):void{
			progressBar.setProgress(event.bytesLoaded,event.bytesTotal); 
			progressBar.label = "共"+event.bytesTotal + "字节--上传进度：" + int(event.bytesLoaded/event.bytesTotal*100) + "%";
		}
		
		private function uploadCompleteHandler(event:Event):void{
			
		}
		
		private function uploadIOErrorHandler(event:IOErrorEvent):void{
			
		}
		
		private function uploadSecurityErrorHandler(event:SecurityErrorEvent):void{
			
		}
		
		private function uploadCompleteDataHandler(event:DataEvent):void{
			var e:DataEvent = new DataEvent(event.type, event.bubbles, event.cancelable, event.data);
			this.dispatchEvent(e);
		}
	}
}